package com.quantgroup.data.config;

import com.quantgroup.data.metrics.DefaultCollector;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.CollectorRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 初始化 Prometheus
 * @author yangjinhua
 */
@Slf4j
@Component
public class InitPrometheus implements ApplicationListener<ContextRefreshedEvent> {
    @Resource
    PrometheusMeterRegistry meterRegistry;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.warn("--------listener  start-------------");
        CollectorRegistry prometheusRegistry = meterRegistry.getPrometheusRegistry();
        prometheusRegistry.register(DefaultCollector.requestErrorCounter);
        prometheusRegistry.register(DefaultCollector.requestCounter);
        prometheusRegistry.register(DefaultCollector.sinkErrorCounter);
        prometheusRegistry.register(DefaultCollector.requestSpendCollector);
        prometheusRegistry.register(DefaultCollector.sinkSpendCollector);
        prometheusRegistry.register(DefaultCollector.unitCalcSpendCollector);
        prometheusRegistry.register(DefaultCollector.recallCalcErrorCounter);
        prometheusRegistry.register(DefaultCollector.recallCalcSuccessCounter);
        prometheusRegistry.register(DefaultCollector.preCalcSuccessCounter);
        prometheusRegistry.register(DefaultCollector.modelCodeRequestCounter);
        log.warn("--------listener  end-------------");
    }
}
